package com.learn.xueer.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.PhoneUtils
import com.learn.xueer.app.base.BaseActivity
import com.learn.xueer.app.weight.recyclerview.SpaceItemDecoration
import com.learn.xueer.data.bindadapter.CustomBindAdapter
import com.learn.xueer.data.model.bean.PramsJson
import com.learn.xueer.databinding.ActivityMemberCenterBinding
import com.learn.xueer.databinding.ActivityServiceDetailBinding
import com.learn.xueer.ui.adapter.BookAdapter
import com.learn.xueer.ui.adapter.PriceAdapter
import com.learn.xueer.ui.adapter.VipAdapter
import com.learn.xueer.ui.viewmodel.MineViewModel
import kotlinx.android.synthetic.main.activity_member_center.rv_vip
import kotlinx.android.synthetic.main.activity_service_detail.iv_pic
import kotlinx.android.synthetic.main.activity_service_detail.lin_price
import kotlinx.android.synthetic.main.activity_service_detail.rv_book
import kotlinx.android.synthetic.main.activity_service_detail.rv_price
import kotlinx.android.synthetic.main.activity_service_detail.tv_buy
import me.hgj.jetpackmvvm.util.get
import me.hgj.jetpackmvvm.util.startActivity

/**
 * 服务详情
 */
class ServiceDetailActivity : BaseActivity<MineViewModel, ActivityServiceDetailBinding>() {

    private val priceAdapter: PriceAdapter by lazy { PriceAdapter(arrayListOf(), -1) }
    private val bookAdapter: BookAdapter by lazy { BookAdapter(arrayListOf()) }
    var id: String = ""
    override fun createObserver() {

        mViewModel.vipDetail.observe(this, Observer {
            CustomBindAdapter.cornerImageUrl(iv_pic, it.info.image)
            if (!it.is_pay) {
                priceAdapter.setList(arrayListOf(PramsJson(it.info.mouth, it.info.price)))
            } else {
                priceAdapter.setList(it.info.pramsjson)
            }
            if (priceAdapter.data.size == 0) {
                lin_price.visibility = View.GONE
            } else {
                lin_price.visibility = View.VISIBLE
            }
        })
        mViewModel.hearSayResult.observe(this, Observer {
            bookAdapter.setList(it)

        })

    }

    inner class ProxyClick {
        fun back() {
            finish()
        }

        fun server() {
            startActivity<ContactServerActivity>()
            //dial : 跳至拨号界面
            //call : 拨打 phoneNumber
//            PhoneUtils.dial("")
//            PhoneUtils.call("")
        }

        fun buy() {
            mViewModel.createOrder()
        }


    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.click = ProxyClick()
        mDatabind.viewmodel = mViewModel
        rv_price.layoutManager = GridLayoutManager(this, 3)
        rv_price.adapter = priceAdapter
        priceAdapter.setOnItemClickListener { baseQuickAdapter, view, i ->
            priceAdapter.select = i
            priceAdapter.notifyDataSetChanged()
            var get = priceAdapter.data.get(i)
            tv_buy.text = "立即购买(${get.price}元)"
        }
        rv_book.addItemDecoration(SpaceItemDecoration(0, ConvertUtils.dp2px(8f)))
        rv_book.adapter = bookAdapter
        id = intent.get<String>("id").toString()
        var grade = intent.get<String>("grade").toString()
        mViewModel.getVipInfo(id)
        mViewModel.bookSectionReq(grade)


    }
}